#include "util.h"
#include "edits_panel.h"
#include "dbase.h"
#include "app_frame.h"

EditsPanel::EditsPanel(AppFrame *top_frame, wxWindow* parent)
    :NotesPanelBase(top_frame, parent)
{
    dbgenter("EditsPanel::EditsPanel()\n")
    this->top_frame = top_frame;
}

void EditsPanel::SetFilter(const NoteFilter& filter)
{
    dbgenter("EditsPanel::SetFilter(%s)\n", filter.ToString());

    int note_count = 0;
    for (unsigned x=0; x<top_frame->edited_notes.size(); x++) {
        if (filter.Matches(top_frame->edited_notes[x])) {
            items.push_back(TextItem(top_frame->edited_notes[x]));
            note_count++;
            if (note_count == filter.max_notes)
                break;
        }
    }
    UpdateDisplay();
    if (items.size() >= 1)
        SetSelection(0);
}

void EditsPanel::GetFilter(NoteFilter& filter)
{
    dbgenter("EditsPanel::GetFilter()\n");
    filter = this->filter;
}

void EditsPanel::NotifyNewNote(const Note& n)
{
    dbgenter("EditsPanel::NotifyNewNote(%s)\n", n.ToString());
    if (filter.Matches(n))
        InsertItemAt(TextItem(n), 0);
    if ((int)items.size() > filter.max_notes)
        RemoveItemAt(filter.max_notes);
}


void EditsPanel::NotifyEditedNote(const Note& n)
{
    dbgenter("EditsPanel::NotifyEditedNote(%s)\n", n.ToString());

    if (ContainsNoteID(n.id)) {
        wxASSERT(filter.Matches(items[FindNoteIndexByID(n.id)].note()));
        RemoveNote(n);
    }

    if (filter.Matches(n))
        InsertItemAt(TextItem(n), 0);
}

void EditsPanel::NotifyDeletedNote(const Note& n)
{
    dbgenter("EditsPanel::NotifyDeletedNote(%s)\n", n.ToString());

    if (ContainsNoteID(n.id)) {
        wxASSERT(filter.Matches(n));
        RemoveNote(n);
    }
}
